package com.b511.swpu.cementbackend.service.impl;

import com.b511.swpu.cementbackend.dto.input.BA_WellInputDto;
import com.b511.swpu.cementbackend.dto.input.WorkBaseInputDto;
import com.b511.swpu.cementbackend.entity.BA_Well;
import com.b511.swpu.cementbackend.repository.*;
import com.b511.swpu.cementbackend.service.WorkBaseService;
import com.b511.swpu.cementbackend.util.R;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.sound.midi.Soundbank;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

@Service
public class WorkBaseServiceImpl implements WorkBaseService {
    private final WorkBaseNewRepository workBaseNewRepository;
    private final BA_WellRepository ba_wellRepository;

    public WorkBaseServiceImpl(WorkBaseNewRepository workBaseNewRepository, BA_WellRepository ba_wellRepository) {
        this.workBaseNewRepository = workBaseNewRepository;
        this.ba_wellRepository = ba_wellRepository;
    }

    @Override
    public R getWorkByCondition(WorkBaseInputDto workBaseInputDto, PageRequest pageRequest) throws Exception {
        WorkBaseRepository repository=new WorkBaseRepository();
        return R.ok().putData(repository.findWorkBase(workBaseInputDto, pageRequest));
    }

    @Override
    public R getCountry() {
        return R.ok().putData(workBaseNewRepository.findCountry());
    }

    @Override
    public R getArea() {
        return R.ok().putData(workBaseNewRepository.findArea());
    }

    @Override
    public R getCompany() {
        return R.ok().putData(workBaseNewRepository.findCompany());
    }

    @Override
    public R getBlock() {
        return R.ok().putData(workBaseNewRepository.findBlock());
    }

    @Override
    public R getOilField() {
        return R.ok().putData(workBaseNewRepository.findOilField());
    }

    @Override
    public R getWellType() {
        return R.ok().putData(workBaseNewRepository.findWellType());
    }

    @Override
    public R getWellSpacing() {
        return R.ok().putData(workBaseNewRepository.findWellSpacing());
    }

    @Override
    public R buildWell(BA_Well ba_well) {
        //判断井是否已存在
        BA_Well database_ba=ba_wellRepository.findByWellName(ba_well.getWellName());
        //判断井名、国家、区块等字符串的值是否重复，若重复不符合要求，需要重新输入
        Set result=new HashSet(5);
        result.add(ba_well.getWellName());
        result.add(ba_well.getCountry());
        result.add(ba_well.getArea());
        result.add(ba_well.getBlock());
        result.add(ba_well.getOilField());
        if((database_ba==null)&&(result.size()==5)){
            ba_wellRepository.save(ba_well);
            return R.ok();
        } else if(database_ba!=null){
            return R.error(801,"该井已存在，请修改井名");
        }else {
            return R.error(802,"井名、国别、区块、区域、油田的值应互不相等");
        }
    }

    @Override
    public R deleteWell(String wellName) {
        ba_wellRepository.deleteByWellName(wellName);
        return R.ok();
    }

    @Override
    public R getWell(String wellName) {
        return R.ok().putData(ba_wellRepository.findByWellName(wellName));
    }

    @Override
    public R putWell(BA_WellInputDto ba_WellInputDto) {
        Optional<BA_Well> optional = ba_wellRepository.findById(ba_WellInputDto.getBa_WELL_ID());
        if(optional.isPresent()){
            BA_Well ba_well=optional.get();
            ba_well.setWellName(ba_WellInputDto.getWellName());
            ba_well.setCountry(ba_WellInputDto.getCountry());
            ba_well.setArea(ba_WellInputDto.getArea());
            ba_well.setBlock(ba_WellInputDto.getBlock());
            ba_well.setOilField(ba_WellInputDto.getOilField());
            ba_well.setCementCompany(ba_WellInputDto.getCementCompany());
            ba_well.setWellType(ba_WellInputDto.getWellType());
            ba_well.setWellSpacing(ba_WellInputDto.getWellSpacing());
            ba_wellRepository.save(ba_well);
        }
        return R.ok();
    }
}
